/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocator xmlns:vm="clr-namespace:VisionAppManager"
                           x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

  You can also use Blend to do all this with the tool's support.
  See http://www.galasoft.ch/mvvm
*/

using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using VisionAppManager.Model;
using VisionApp.Interface;
using Autofac;
//using Microsoft.Practices.ServiceLocation;

namespace VisionAppManager.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view services and models
            ////    SimpleIoc.Default.Register<IDataService, DesignDataService>();
            ////}
            ////else
            ////{
            ////    // Create run time view services and models
            ////    SimpleIoc.Default.Register<IDataService, DataService>();
            ////}

            //SimpleIoc.Default.Register<MainViewModel>();
            //SimpleIoc.Default.Register<VMLog>();
            //SimpleIoc.Default.Register<VMRibbonMenu>();
            //SimpleIoc.Default.Register<VMCfg>();
            //SimpleIoc.Default.Register<VMStatusBar>();
            //SimpleIoc.Default.Register<VMSearch>();
            //SimpleIoc.Default.Register<VMImgViewer>();
            //SimpleIoc.Default.Register<VMImgViewerCam1>();
            //SimpleIoc.Default.Register<VMImgViewerCam2>();
            //SimpleIoc.Default.Register<VMHelp>();
            //SimpleIoc.Default.Register<VMStatistic>();
            //SimpleIoc.Default.Register<IWorker, Worker>();
           
            AppEngine.Builder.RegisterType<MainViewModel>().SingleInstance();
            AppEngine.Builder.RegisterType<VMLog>().SingleInstance();
            AppEngine.Builder.RegisterType<VMRibbonMenu>().SingleInstance();
            AppEngine.Builder.RegisterType<VMCfg>().SingleInstance();
            AppEngine.Builder.RegisterType<VMStatusBar>().SingleInstance();
            AppEngine.Builder.RegisterType<VMSearch>().SingleInstance();
            AppEngine.Builder.RegisterType<VMImgViewer>().SingleInstance();
            AppEngine.Builder.RegisterType<VMImgViewerCam1>().SingleInstance();
            AppEngine.Builder.RegisterType<VMImgViewerCam2>().SingleInstance();
            AppEngine.Builder.RegisterType<VMHelp>().SingleInstance();
            AppEngine.Builder.RegisterType<VMStatistic>().SingleInstance();
            AppEngine.Builder.RegisterType<Worker>().As<IWorker>().SingleInstance();
        }

        public MainViewModel Main
        {
            get
            {
                //ServiceLocator.Current.GetInstance<VMLog>();
                //ServiceLocator.Current.GetInstance<MainViewModel>().VMWorkspace = ServiceLocator.Current.GetInstance<VMImgViewerCam1>();
                //ServiceLocator.Current.GetInstance<MainViewModel>().VMWorkspace = ServiceLocator.Current.GetInstance<VMImgViewerCam2>();
                //ServiceLocator.Current.GetInstance<MainViewModel>().VMWorkspace = ServiceLocator.Current.GetInstance<VMImgViewer>();
                AppEngine.Container.Resolve<VMLog>();
                AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMImgViewerCam1>();
                AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMImgViewerCam2>();
                AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMImgViewer>();
                return AppEngine.Container.Resolve<MainViewModel>();
            }
        }

        
        public static void Cleanup()
        {
            // TODO Clear the ViewModels
            AppEngine.Container.Resolve<VMRibbonMenu>().Cleanup();
            //ServiceLocator.Current.GetInstance<VMRibbonMenu>().Cleanup();
        }
    }
}